import folium
import pandas as pdCreating a Map in Python for Plotting
Import Libraries
Foliumallows you to create interactive leaflet mapsPandasallows for reading CSV files and turning it into a DataFrame
Read the Excel file
data = pd.read_excel("United_States_Offense_Type_by_Agency_2022.xlsx", sheet_name='2022 Cities with Location')
# dataCreating Dropdown
Import Libraries for Dropdown Widgets
import ipywidgets as widgets
from IPython.display import displayOptions for Dropdown
Users have the option of selecting from - Total Offenses - Crimes Against Persons - Crimes Against Property - Crimes Against Society
They are required to select one of the options. If they don’t select any of them, the code will not run correctly.
options = ["Total\nOffenses", "Crimes\nAgainst\nPersons", "Crimes\nAgainst\nProperty", "Crimes\nAgainst\nSociety"]Create the dropdown widget
dropdown = widgets.Dropdown(options=options, description='Filter:')Define a function the handle the selection
Anytime a selection is made, the selection is shown as feedback to the user
def on_change(change):
if change['type'] == 'change' and change['name'] == 'value':
print("Selected:", change['new'])Attach the function to the dropdown’s event handler
dropdown.observe(on_change)Display the dropdown widget
display(dropdown)
dropdown.value'Total\nOffenses'
Create a Map of the U.S.
Using folium library to create a map that starts the user looking at the U.S.
map_us = folium.Map(location=[37.0902, -95.7129], zoom_start=4, control_scale=True)
title_html = """<h3 align="center" style="font-size:20px">{}</h3>""".format("2022 Cities: " + dropdown.value)
map_us.get_root().html.add_child(folium.Element(title_html))<branca.element.Element at 0x1553ecd50>
Creating the Map
We’re looping through the Excel data that was read earlier. We retrieve and store the longitude and latitude values of every city that appears in the Excel sheet. If there is a NaN value that appears in the data, it is skipped over.
Based on the selection made earlier in the dorpdown, the selection is assigned to the variable value. This basically tells folium what we want to see on the map.
We then use the CircleMarker function of the folium library to plot points upon the map. The size of the points being plotted is based on the corresponding values of dropdown selection.
for index, row in data.iterrows():
#Location
location = row['Location']
#Latitude
lat = row['Latitude']
# if latitude is NaN
if pd.isna(lat):
continue
#Longitude
lon = row['Longitude']
# Value being displayed based on dropdown selection
value = row[dropdown.value]
folium.CircleMarker(location=[lat, lon],
radius=value/5000, color='rgb(178,34,34)',
fill=True, fill_color='rgb(178,34,34)',
fill_opacity=0.3,
weight=1,
tooltip=f"Location: {location} \n Value: {value}").add_to(map_us)Display of Map
map_usimport ipywidgets as widgets
from IPython.display import display
# Define the dropdown options
options = ["Option 1", "Option 2", "Option 3"]
# Create the dropdown widget
dropdown = widgets.Dropdown(options=options, description='Select option:')
# Define a function to handle the dropdown value change
def handle_dropdown_change(change):
print("Selected:", change.new)
# Attach the function to the dropdown's value change event
dropdown.observe(handle_dropdown_change, names='value')
# Display the dropdown widget
display(dropdown)